package org.mysfit.ctp4j.api.controller.v1;

import org.mysfit.ctp4j.api.vo.QuoteVO;
import org.mysfit.ctp4j.core.MyCThostFtdcMdSpi;
import org.mysfit.ctp4j.core.MyCThostFtdcTraderSpi;
import org.mysfit.ctp4j.core.service.CommonService;
import org.mysfit.ctp4j.core.service.MarketService;
import org.mysfit.ctp4j.core.service.TradeService;
import org.mysfit.ctp4j.infra.pojo.ApiResponse;
import org.mysfit.ctp4j.infra.pojo.CtpConfig;
import org.mysfit.ctp4j.infra.pojo.CtpUser;
import org.mysfit.ctp4j.infra.utils.TradeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import ctp.thosttraderapi.CThostFtdcMdApi;
import ctp.thosttraderapi.CThostFtdcTraderApi;
import ctp.thosttraderapi.THOST_TE_RESUME_TYPE;

/**
 * 
 * @author long.gf
 * @since 2022-12-06
 *
 */
@RestController
@RequestMapping("/v1/trade")
public class TradeController {

	private static final Logger LOGGER = LoggerFactory.getLogger(TradeController.class);

	@Autowired
	private MarketService marketService;
	
	@Autowired
	private TradeService tradeService;
	
	@Autowired
	private CommonService commonService;
	
	private void initMarket(CtpConfig ctpConfig, CtpUser ctpUser) {
		CThostFtdcMdApi mdApi = CThostFtdcMdApi.CreateFtdcMdApi();
		MyCThostFtdcMdSpi mdSpi = new MyCThostFtdcMdSpi(ctpUser, mdApi, marketService);
		mdApi.RegisterSpi(mdSpi);
		mdApi.RegisterFront(ctpConfig.getMarketFront());
		mdApi.Init();
		mdApi.Join();
	}
	
	private void initTrade(CtpConfig ctpConfig, CtpUser ctpUser) {
		CThostFtdcTraderApi traderApi = CThostFtdcTraderApi.CreateFtdcTraderApi();
		MyCThostFtdcTraderSpi traderSpi = new MyCThostFtdcTraderSpi(ctpUser, traderApi, tradeService);
		traderApi.RegisterSpi(traderSpi);
		traderApi.RegisterFront(ctpConfig.getTradeFront());
		traderApi.SubscribePublicTopic(THOST_TE_RESUME_TYPE.THOST_TERT_QUICK);
		traderApi.SubscribePrivateTopic(THOST_TE_RESUME_TYPE.THOST_TERT_QUICK);
		traderApi.Init();
		traderApi.Join();
	}
	
	@GetMapping("/initTrade/{investorId}")
	public ApiResponse<String> quoteInert(@PathVariable("investorId")String investorId) throws Exception {
		CtpConfig ctpConfig = commonService.getConfig();
		CtpUser ctpUser = commonService.getUser(investorId);
		new CtpUser(ctpUser);
		new Thread(() -> {
			this.initTrade(ctpConfig, ctpUser);
		}).start();
		new Thread(() -> {
			this.initMarket(ctpConfig, ctpUser);
		}).start();
		return ApiResponse.ok();
	}
	
	@PostMapping("/doOrder")
	public ApiResponse<String> doOrder(@RequestBody QuoteVO q) {
		new Thread(() -> {
			try {
				tradeService.doCtpOrder(q);
 				TradeUtils.addQuoteReq(q);
			} catch (Exception e) {
				LOGGER.error("quteo异常",e);
 			}
		}).start();
		return ApiResponse.ok();
	}
	
}
